package com.wxdc.nmgyt.nmgytwydc.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.base.BasePersenter;
import com.wxdc.nmgyt.nmgytwydc.base.BaseView;
import com.wxdc.nmgyt.nmgytwydc.common.MyCircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.dagger.component.ActivityComponent;
import com.wxdc.nmgyt.nmgytwydc.dagger.component.DaggerActivityComponent;
import com.wxdc.nmgyt.nmgytwydc.dagger.module.ActivityModule;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/21.
 */

public abstract class BaseActivity<T extends BasePersenter> extends AppCompatActivity implements BaseView {

    protected abstract int getLayout();

    protected abstract void initInject();

    @Inject
    protected T mPersenter;
    protected Activity mContext;
    private Unbinder mUnBinder;
    protected MyApp mApp;
    protected CircleProgressDialog mCircleProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        mUnBinder = ButterKnife.bind(this);
        mApp = MyApp.getInstance();
        mContext = this;
        initInject();
        if (mPersenter != null) {
            mPersenter.attachView(this);
        }
        initProgressDialog();

    }

    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(mApp.getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    /*
    * 定义progressdialog
    * */
    private CircleProgressDialog initProgressDialog() {
        if (mCircleProgressDialog == null) {
            mCircleProgressDialog = new CircleProgressDialog(mContext);
            mCircleProgressDialog.setCancelable(false);
            mCircleProgressDialog.setText("加载中...");
            MyCircleProgressDialog.initCircleProgressDialog(mCircleProgressDialog);
        }
        return mCircleProgressDialog;
    }

    /*
    * 获取progressdialog
    * */
    protected CircleProgressDialog getCircleProgressDialog() {
        return mCircleProgressDialog;
    }
    /*
    * 显示progressdialog
    * */
    protected void showProgressDialog(){
        if (mCircleProgressDialog == null) {
            initProgressDialog();
        }
        mCircleProgressDialog.showDialog();
    }
    /*
   * 显示progressdialog
   * */
    protected void showProgressDialog(String msg){
        if (mCircleProgressDialog == null) {
            initProgressDialog();
        }
        mCircleProgressDialog.changeText(msg);
        mCircleProgressDialog.showDialog();
    }
    /*
    * 隐藏progressdialog
    * */
    protected  void hideProgressDialog(){
        if (mCircleProgressDialog != null && mCircleProgressDialog.isShowing()) {
            mCircleProgressDialog.dismiss();
        }
    }
    protected  boolean isProgressDialog(){
        return mCircleProgressDialog == null ? false : mCircleProgressDialog.isShowing();
    }
    @Override
    protected void onStart() {
        super.onStart();

    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPersenter != null) {
            mPersenter.detachView();
        }
        mUnBinder.unbind();
        hideProgressDialog();
    }
}
